//
//  ViewController.m
//  iRSocketTest
//
//  Created by 胡鹏飞 on 15/8/7.
//  Copyright (c) 2015年 胡鹏飞. All rights reserved.
//

#import "ViewController.h"
#include <ifaddrs.h>
#include <arpa/inet.h>
@interface ViewController ()
<
UIAlertViewDelegate
>
{
    NSData *data;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _socket = [iRSocket defaultSocket];
    _socket.delegate = self;

    
    UIButton * connectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    connectBtn.frame = CGRectMake(50, 100, 220, 50);
    [connectBtn setTitle:@"链接" forState: UIControlStateNormal];
    [connectBtn addTarget:self action:@selector(connectTheSocket) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:connectBtn];
    
    UIButton * sendBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    sendBtn.frame = CGRectMake(50, 200, 220, 50);
    [sendBtn setTitle:@"发送" forState: UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sendBtn];
    
}

- (void)sendMessage
{

     NSMutableArray *dataSource = [[NSMutableArray alloc] initWithObjects:@"254", @"254", @"0", @"0", @"0", @"150", @"100",@"105",@"200",@"0",@"0",@"60",@"60", @"170",  @"170",  @"170",    nil];
    _socket.dataSource = dataSource;
    [_socket sendMessage];

}

- (void)connectTheSocket
{

    BOOL is = [_socket connectTheServerWithIP:@"192.168.222.254" andPortNum:34952];
    NSLog(@"is connected = %d", is);
    
}


#pragma mark -- iRsocket delegate
- (void)sendMessageSuccessed:(iRSocket *)socket
{
    NSLog(@"发送成功");
}

- (void)connectSuccess:(iRSocket *)socket
{
    NSLog(@"socket.ip = %@", socket.Ip);
}

- (void)receivedMessage:(NSString *)message
{
    NSLog(@"message = %@", message);
}

- (void)connectFailedWithError:(NSError *)error
{
    NSLog(@"connect faile with error %@", error.domain);
}

- (void)sendMessageFailedWithError:(NSError *)error
{
    NSLog(@"send Message faile with error %@", error.domain);
}

- (void)sockeDisconnected:(iRSocket *)socket
{
    NSLog(@"dis connented");
}
@end
